package vn.vfriends.vbox.provider;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlRootElement;
import org.scribe.utils.OAuthEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.vbox.util.ProviderUtil;

/**
 *
 * @author tuancn
 */
public class DropBoxProvider implements StorageProvider {

    private static final Logger logger = LoggerFactory.getLogger(DropBoxProvider.class);
    
    private static final String API_KEY = ProviderUtil.getProperties().getProperty("DROPBOX_API_KEY");
    
    private static final String API_SECRET = ProviderUtil.getProperties().getProperty("DROPBOX_API_SECRET");
    
    private static final String CALLBACK_URL = ProviderUtil.getProperties().getProperty("DROPBOX_CALLBACK_URL");
    
    private static final String AUTHORIZATION_URL = "https://www.dropbox.com/1/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s";
    
    private String accessToken;
    
    private String ownId;
    
    private String name;
    
    public DropBoxProvider() {
    }
    
    @Override
    public String getAuthorizationUrl() {
        return String.format(AUTHORIZATION_URL, API_KEY, OAuthEncoder.encode(CALLBACK_URL));
    }

    @Override
    public void init(String code) {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("https://api.dropbox.com/1/oauth2/token");
        
        Form form = new Form();
        form.param("code", code);
        form.param("grant_type", "authorization_code");
        form.param("client_id", API_KEY);
        form.param("client_secret", API_SECRET);
        form.param("redirect_uri", CALLBACK_URL);
        
        DropboxTokenResponse res = target.request(MediaType.APPLICATION_JSON_TYPE)
                .post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), DropboxTokenResponse.class);
        this.accessToken = res.getAccess_token();
        this.ownId = res.getUid();
        this.name = res.getDisplay_name();
    }

    @Override
    public String getOwnId() {
        return this.ownId;
    }

    @Override
    public String getAccessToken() {
        return accessToken;
    }

    @Override
    public String getName() {
        return name;
    }
    
    @Override
    public String getRefreshToken() {
        return "";
    }
    
    @Override
    public long getExpiresIn() {
        return 0;
    }
}

@XmlRootElement
class DropboxTokenResponse {

    private String access_token;

    private String uid;
    
    private String display_name;

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getDisplay_name() {
        return display_name;
    }

    public void setDisplay_name(String display_name) {
        this.display_name = display_name;
    }
}
